package cn.itcast.bos.service.transit.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.take_delivery.WayBillRepository;
import cn.itcast.bos.dao.transit.TransitInfoRepository;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.domain.transit.TransitInfo;
import cn.itcast.bos.index.WayBillIndexRepository;
import cn.itcast.bos.service.transit.TransitInfoService;

@Service
@Transactional
public class TransitInfoServiceImpl implements TransitInfoService {

	@Autowired
	private TransitInfoRepository transitInfoRepository;
	
	@Autowired
	private WayBillRepository wayBillRepository;
	
	@Autowired
	private WayBillIndexRepository wayBillIndexRepository;
	

	@Override
	public void createTransit(String wayBillIds) {
		if(StringUtils.isNotBlank(wayBillIds)){
			String[] wayBillIdsArray = wayBillIds.split(",");
			for (String wayBillId : wayBillIdsArray) {
				WayBill wayBill = wayBillRepository.findOne(Integer.parseInt(wayBillId));
				//判断订单状态是否为待发货(signStatus=1)
				if(wayBill.getSignStatus()==1){
					
					TransitInfo transitInfo = new TransitInfo();
					transitInfo.setWayBill(wayBill);
					transitInfo.setStatus("出入库中转");
					transitInfoRepository.save(transitInfo);
					
					//更改运单状态  --派送中
					wayBill.setSignStatus(2);
					
					//对运单索引库进行更新
					wayBillIndexRepository.save(wayBill);
				}
			}
			
		}
	}

	@Override
	public Page<TransitInfo> pageQuery(Pageable pageable) {
		Page<TransitInfo> pageData = transitInfoRepository.findAll(pageable);
		return pageData;
	}

}
